import { _decorator, Component, Node } from 'cc';
import { ecs } from 'db://oops-framework/libs/ecs/ECS';
import { CCVMParentComp } from 'db://oops-framework/module/common/CCVMParentComp';
import { Match } from '../Match';
import { RoomInfo } from '../../../tsrpc/shared/type/RoomInfo';
import { instantiate } from 'cc';
import { Prefab } from 'cc';
import { RoomListItem } from './RoomListItem';
import { ModuleUtil } from 'db://oops-framework/module/common/ModuleUtil';
import { CreateRoomViewComp } from './CreateRoomViewComp';
import { UIID } from '../../common/config/GameUIConfig';
const { ccclass, property } = _decorator;

@ccclass('MatchViewComp')
@ecs.register('MatchView', false)
export class MatchViewComp extends CCVMParentComp {
    @property(Node)
    roomListContent:Node = null!;
    @property(Prefab)
    roomInfoPrefab:Prefab = null!;
    reset(): void {
        
    }

    async start() {
        let roomList = await this.getEntity<Match>().getRoomList();
        this.showRoomList(roomList);
    }

    showRoomList(roomList: RoomInfo[]) {
        for (let i = 0; i < roomList.length; i++) {
            const roomListItemNode = instantiate(this.roomInfoPrefab);
            roomListItemNode.x = 0;
            roomListItemNode.y = 0;
            roomListItemNode.parent = this.roomListContent;
            const roomListItem = roomListItemNode.getComponent(RoomListItem);
            if (roomListItem) {
                roomListItem.options = {
                    roomInfo: roomList[i],
                    onClick: (v: {serverUrl: string,roomId: number}) => {
                        //
                        console.error("点击加入房间id:",v.roomId);
                    }
                }
            }
            

        }
    }

    onDoJoin() {

    }

    onOpenCreateRoomView() {
        ModuleUtil.addViewUiAsync(this.ent, CreateRoomViewComp, UIID.CreateRoom);
    }
}


